/******************************************************************************
 * Preprocessing JS is fun!
 *****************************************************************************/
#if DEBUG_VERSION
if (!window.console) {
  window.console = {};
}
if (!console.log) {
  var dump = document.createElement("div");
  dump.className = "console";
  document.body.appendChild(dump);
  console.count = 0;
  console.log = function() { 
    var line = document.createElement("div");
    line.className = (console.count++ % 2 == 0) ? "even" : "odd"; 
    var data = [];
    for (var i = 0, arg; arg = arguments[i]; i++) {
      var text = arg.toString && arg.toString() ||
                 arg.text ||
                 arg;
      if (typeof text == "string") {
        text = text.replace(/</g, "&lt;").replace(/>/g, "&gt;");
      }
      data.push(text);
    }
    line.innerHTML = data.join(" ");
    dump.insertBefore(line, dump.firstChild);
    dump.parentNode.removeChild(dump);
    document.body.appendChild(dump); 
  };
} 
#define LOG(args...) console.log(args)
#else
#define LOG(args...) 
#endif

#define RESIZE() gadgets.window.adjustHeight(200); window.setTimeout(function() { gadgets.window.adjustHeight(); }, 100);
 
#if TRACKING
#define TRACK(name) pageTracker._trackPageview('track/' + name)
#define SEGMENT(value) pageTracker._setVar(value);
#else
#define TRACK(name) 
#define SEGMENT(value) 
#endif

/******************************************************************************
 * Define dafos namespace
 *****************************************************************************/
var dafos = {};
/**
 * Binds a function to a certain executable scope.
 */
dafos.bind = function(func, target) {
  var curryparams = [];
  for (var i = 2, len = arguments.length; i < len; i++) {
    curryparams.push(arguments[i]);
  }
  return function() {
    var clonedparams = curryparams.slice();
    for (var j = 0, len = arguments.length; j < len; j++) {
      clonedparams.push(arguments[j]);
    }
    func.apply(target, clonedparams);
  }
};


/**
 * Converts a timestamp to a human-readable string.
 * @param {Number} A unix timestamp.
 * @returns {String} A human-readable version of the time.
 */
dafos.timeToPrettyString = function(ts) {
  if (isNaN(ts)) {
    return "an indeterminate amount of time ago";
  } 
  time = (new Date().getTime() - ts) / 1000;
  if (time < 60) {
    return "less than a minute ago";
  } else if (time < 120) {
    return "about a minute ago";
  } else if (time < 3600) {
    var adjusted = Math.round(time / 60);
    return "about " + adjusted + " minutes ago";
  } else if (time < 7200) {
    return "about an hour ago";
  } else if (time < 86400) {
    var adjusted = Math.round(time / 3600);
    return "about " + adjusted + " hours ago";
  } else if (time < 172800) {
    return "about a day ago";
  } else if (time < 2592000) {
    var adjusted = Math.round(time / 86400);
    return "about " + adjusted + " days ago";    
  } else if (time < 5184000) {
    return "about a month ago";
  } else {
    var adjusted = Math.round(time / 2592000);
    return "about " + adjusted + " months ago";        
  }
};

/**
 * Converts a number representation of a month to a string
 * @param {Number} The number of the month (1 = January)
 * @returns {String} A capitalized month name corresponding with the number
 */
dafos.monthToString = function(month) {
  return [
    "January",
    "Febuary",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
  ][month - 1] || "";
}


